package com.sgcc.fj.pmos.e_fj_home.presenter;

import com.alibaba.android.arouter.utils.TextUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.fj.pmos.common.base.BasePresenter;
import com.sgcc.fj.pmos.common.http.HttpInterConst;
import com.sgcc.fj.pmos.common.http.HttpRequestUtils;
import com.sgcc.fj.pmos.common.http.MvpNetCallback;
import com.sgcc.fj.pmos.common.utils.ListPageUtil;
import com.sgcc.fj.pmos.common.utils.UserManager;
import com.sgcc.fj.pmos.e_fj_home.bean.ByPublishTypeBean;
import com.sgcc.fj.pmos.e_fj_home.bean.DataJYBean;
import com.sgcc.fj.pmos.e_fj_home.bean.TradingInstitutionBasicinfoBean;
import com.sgcc.fj.pmos.e_fj_home.contract.TradingInstitutionBasicinfoContract;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class TradingInstitutionBasicinfoPresenter extends BasePresenter<TradingInstitutionBasicinfoContract.View>
        implements TradingInstitutionBasicinfoContract.Presenter {
    @Override
    public void getData() {
        Map<String, Object> params = new HashMap<>();
        String infoCodes[] = {"jyjgJbxx"};
        params.put("infoCodes", infoCodes);
        params.put("marketId", "PDBLN");
        HttpRequestUtils.getInstance().request(params, HttpInterConst.INFORMATION_DISCLOSURE_GETBYCONDITIONBYPUBLISHTYPE, new MvpNetCallback<ByPublishTypeBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
            }

            @Override
            public void onResponse(ByPublishTypeBean response, int id) {
                super.onResponse(response, id);
                if (response != null) {
                    if ("0".equals(response.getStatus())) {
                        StringBuffer stringBuffer = new StringBuffer();
                        if (response.getData() != null) {
                            if (response.getData().size() > 0) {
                                for (ByPublishTypeBean.DataBean bean : response.getData()) {
                                    stringBuffer.append(bean.getGuid() + ",");
                                }

                                String informationId = stringBuffer.substring(0, stringBuffer.length() - 1);
                                getDataJY(informationId);
                            }
                        }
                    } else {
                        if (!TextUtils.isEmpty(response.getMessage())) {
                            ToastUtils.showShort(response.getMessage());
                        }
                    }
                }
            }
        });
    }

    private void getDataJY(String informationId) {
        Map<String, Object> params = new HashMap<>();
        String informationIds[] = informationId.split(",");
        Map<String, Object> data = new HashMap<>();
        String operateTimeArr[] = {};
        data.put("operateTimeArr", operateTimeArr);
        data.put("customInfoName", "");
        data.put("sourceName", "");
        data.put("marketId", "PDBLN");
        data.put("informationIds", informationIds);
        params.put("data", data);
        Map<String, Object> pageInfo = new HashMap<>();
        String pageSizes1[] = {"20", "50", "100", "200", "500", "1000"};
        int pageSizes[] = {20, 50, 100, 200, 500, 1000};
        pageInfo.put("pageSizes1", pageSizes1);
        pageInfo.put("pageSizes", pageSizes);
        pageInfo.put("pageSize", 10);
        pageInfo.put("pageNum", 1);
        params.put("pageInfo", pageInfo);
        HttpRequestUtils.getInstance().request(params, HttpInterConst.INFORMATION_DISCLOSURE_GETDATAJY, new MvpNetCallback<DataJYBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
            }

            @Override
            public void onResponse(DataJYBean response, int id) {
                super.onResponse(response, id);
                if (response != null) {
                    if ("0".equals(response.getStatus())) {
                        if (response.getData() != null) {
                            if (response.getData().getList() != null) {
                                if (response.getData().getList().size() > 0) {
                                    queryById(response.getData().getList().get(0).getGuid());
                                }
                            }
                        }
                    } else {
                        if (!TextUtils.isEmpty(response.getMessage())) {
                            ToastUtils.showShort(response.getMessage());
                        }
                    }
                }
            }
        });
    }

    private void queryById(String guid) {
        Map<String, Object> params = new HashMap<>();
        params.put("guid", guid);
        HttpRequestUtils.getInstance().request(params, HttpInterConst.INFORMATION_DISCLOSURE_QUERYBYID, new MvpNetCallback<TradingInstitutionBasicinfoBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
            }

            @Override
            public void onResponse(TradingInstitutionBasicinfoBean response, int id) {
                super.onResponse(response, id);
                if (response != null) {
                    if ("0".equals(response.getStatus())) {
                        getView().getDataSuccess(response);
                    } else {
                        if (!TextUtils.isEmpty(response.getMessage())) {
                            ToastUtils.showShort(response.getMessage());
                        }
                    }
                }
            }
        });
    }
}
